/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.senac.academicnet.view.curso;

import br.com.senac.academicnet.model.CursoBO;
import br.com.senac.academicnet.model.entidades.CursoVO;
import javax.swing.JOptionPane;

public class CadastroCurso extends javax.swing.JFrame {
    
    public CadastroCurso() {
        initComponents();
 
        this.incluir();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtNome = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnConfirmar = new javax.swing.JButton();
        txtCargaHoraria = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Aluno");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtNome.setName("txtNome"); // NOI18N
        getContentPane().add(txtNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 30, 300, -1));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Nome");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, -1, -1));

        txtCodigo.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("00000000000"))));
        txtCodigo.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtCodigo.setText("0");
        txtCodigo.setName("txtCodigo"); // NOI18N
        getContentPane().add(txtCodigo, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, 50, -1));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setText("Codigo");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        btnCancelar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 150, -1, -1));
        btnCancelar.getAccessibleContext().setAccessibleName("btnCancelar");

        btnAlterar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnAlterar.setLabel("Alterar");
        btnAlterar.setName("btnAlterar"); // NOI18N
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });
        getContentPane().add(btnAlterar, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 150, -1, -1));

        btnExcluir.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.setName("btnExcluir"); // NOI18N
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });
        getContentPane().add(btnExcluir, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 150, -1, -1));

        btnConfirmar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnConfirmar.setLabel("Confirmar");
        btnConfirmar.setName("btnConfirmar"); // NOI18N
        btnConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmarActionPerformed(evt);
            }
        });
        getContentPane().add(btnConfirmar, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 150, -1, -1));

        txtCargaHoraria.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("00000000000"))));
        txtCargaHoraria.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtCargaHoraria.setText("0");
        txtCargaHoraria.setName("txtCodigo"); // NOI18N
        getContentPane().add(txtCargaHoraria, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, 80, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel7.setText("Carga Horaria");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.cancelar();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        this.alterar();
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        this.excluir();
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmarActionPerformed
        this.confirmar();
    }//GEN-LAST:event_btnConfirmarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConfirmar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JFormattedTextField txtCargaHoraria;
    private javax.swing.JFormattedTextField txtCodigo;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables

    public CadastroCurso(CursoVO curso) {
        this();

        this.curso = curso;
        this.visualizar();
    }

    private enum Operacao {
        INCLUSAO,
        ALTERACAO,
        VISUALIZACAO,
        EXCLUSAO
    }
    private Operacao operacao;
    private CursoVO curso = new CursoVO();
    
    private void habilitarEdicao(boolean habilitar) {
        txtCodigo.setEnabled(false);
        txtNome.setEnabled(habilitar);
        txtCargaHoraria.setEnabled(habilitar);
    }
   
    
    private boolean carregarObjeto() {
        boolean retorno = true;

        try {
            curso.setCodigo(Integer.valueOf(txtCodigo.getText()));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Codigo invalido.");
            txtCodigo.setText("0");
            retorno = false;
        }
        
        try {
            curso.setNome(txtNome.getText());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
            retorno = false;
        }
        
        try {
            curso.setCargaHoraria(Integer.valueOf(txtCargaHoraria.getText()));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Carga Horaria invalida.");
            txtCargaHoraria.setText("0");
            retorno = false;
        }

        return retorno;
    }

    private void carregarRegistro() {
        txtCodigo.setText(Integer.toString(curso.getCodigo()));
        txtNome.setText(curso.getNome());
        txtCargaHoraria.setText(Integer.toString(curso.getCargaHoraria()));
    }
 
    private void exibirBotoes() {
        btnAlterar.setVisible(false);
        btnExcluir.setVisible(false);
        btnConfirmar.setVisible(false);
        btnCancelar.setVisible(false);

        switch (operacao) {
            case INCLUSAO:
            case ALTERACAO:
                btnConfirmar.setVisible(true);
                btnCancelar.setVisible(true);
                break;

            case VISUALIZACAO:
                btnAlterar.setVisible(true);
                btnExcluir.setVisible(true);
                break;
        }
    }

    private void incluir() {
        this.operacao = Operacao.INCLUSAO;
        
        this.habilitarEdicao(true);
        this.exibirBotoes();
    }

    private void alterar() {
        this.operacao = Operacao.ALTERACAO;
        this.habilitarEdicao(true);
        this.exibirBotoes();
    }

    private void visualizar() {
        this.operacao = Operacao.VISUALIZACAO;
        this.habilitarEdicao(false);
        this.carregarRegistro();
        this.exibirBotoes();
    }

    private void confirmar() {
        if (this.carregarObjeto()) {
            switch (operacao) {
                case INCLUSAO:
                    try {
                        CursoBO.incluir(curso);

                        JOptionPane.showMessageDialog(this, "Registro incluido!");
                        this.cancelar();
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(this, "Erro ao incluir: " + ex.getMessage());
                    }

                    break;

                case ALTERACAO:
                    try {
                        CursoBO.alterar(curso);
                        JOptionPane.showMessageDialog(this, "Registro alterado!");
                        this.visualizar();
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(this, "Erro ao alterar: " + ex.getMessage());
                    }
                    break;
            }
        }
    }

    private void cancelar() {
        switch (operacao) {
            case INCLUSAO:
            case EXCLUSAO:
                this.setVisible(false);
                this.dispose();
                break;

            case ALTERACAO:
                this.visualizar();
                break;
        }
    }

    private void excluir() {
        if (JOptionPane.showConfirmDialog(this, "Confirma exclusao?") == 0) {
            try {
                CursoBO.deletar(curso);
                JOptionPane.showMessageDialog(this, "Registro excluido!");
                operacao = Operacao.EXCLUSAO;
                this.cancelar();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Erro ao excluir: " + ex.getMessage());
            }
        }
    }
}
